<?php
/**
 * 视图基类
 * 
 * 用于定义模板的绘制和头部输出.
 * @abstract
 * @author Leo Ning <leo.ning@like18.com> 2010-11-14 12:25
 * @version 1.0
 * @since 1.0
 * @copyright 2008-2011 LIKE18 INC.
 * @package view
 */
abstract class View {
	protected $tpl = '';
	protected $action = null;
	protected $data;
	protected $layout = null;
	public function __construct($data = array(), $tpl = '') {
		if (is_string ( $data )) {
			$this->setTpl ( $data );
			$this->data = array ();
		} else {
			$this->setTpl ( $tpl );
			$this->data = $data;
		}		
	}
	/**
	 * 绘制
	 * @param array $data
	 * @param Response $response 
	 * @param string $tpl 模板
	 * @return string	 
	 */
	public abstract function render($action, $module);
	/**
	 * 
	 * 设置输出头
	 */
	public function setHeader() {
	}
	/**
	 * 设置模板
	 * @param string $tpl
	 */
	public function setTpl($tpl) {
		if ($tpl) {
			$this->tpl = str_replace ( '/', DS, $tpl );
		}
	}
	public function setDefaultTpl($tpl) {
		if ($tpl && empty ( $this->tpl )) {
			$this->tpl = str_replace ( '/', DS, $tpl );
		}
	}
	/**
	 * 
	 * 向视图传值
	 * @param mixed $value
	 */
	public function assign($name, $value = '') {
		if (is_array ( $name )) {
			$this->data = array_merge_recursive ( $this->data, $name );
		} else if (preg_match ( '#[_a-z][a-z0-9_]*#i', $name )) {
			$this->data [$name] = $value;
		}
	}
	/**
	 * 返回视图中的数据
	 * @return array
	 */
	public function getData() {
		return $this->data;
	}
}